class Command {
    constructor(receiver) {
        this.receiver = receiver
    }
    execute () {
        throw new Error('请重写execute方法')
    }
}

class ConcreteCommandA extends Command {
    execute () {
        this.receiver.actionA()
    }
}

class ConcreteCommandB extends Command {
    execute () {
        this.receiver.actionB()
    }
}

class Receiver {
    actionA () {
        console.log('actionA')
    }
    actionB () {
        console.log('actionB')
    }
}

class Invoker {
    constructor() {
        this.command = new Map()
    }
    setCommand (key, command) {
        this.command.set(key, command)
    }
    executeCommand (key) {
        const command = this.command.get(key)
        if (!command) {
            console.log('没有找到命令')
            return
        }
        command.execute()
    }
}

const receiver = new Receiver()
const invoker = new Invoker()

invoker.setCommand('A', new ConcreteCommandA(receiver))
invoker.setCommand('B', new ConcreteCommandB(receiver))

invoker.executeCommand('A')
invoker.executeCommand('B')